﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.Xml;

namespace SceneViewLoader
{
    public partial class SceneViewLoader : Form
    {
        OgreConfig ogreConfig;
        XmlDocument appSettings;
        XmlNode root;
        XmlNode ApplicationSettings;
        XmlNode AllowDevConsole;
        XmlNode SandBoxMode;
        XmlNode UseHardwarePhysX;
        XmlNode UseShadows;
        XmlNode LogBenchmark;
        XmlNode ServerAddress;
        XmlNode ServerPort;

        public SceneViewLoader()
        {
            InitializeComponent();

            ogreConfig = new OgreConfig();
            ogreConfig.loadConfig();

            if (ogreConfig.RenderSystem == "Render System=Direct3D9 Rendering Subsystem")
            {
                comboBoxRenderingSysytem.SelectedIndex = 0;
            }
            else
            {
                comboBoxRenderingSysytem.SelectedIndex = 1;
            }

            switch(ogreConfig.FSAA)
            {
                case "FSAA=0":
                    comboBoxAntialiasing.SelectedIndex = 0;
                    break;
                case "FSAA=2":
                    comboBoxAntialiasing.SelectedIndex = 1;
                    break;
                case "FSAA=4":
                    comboBoxAntialiasing.SelectedIndex = 2;
                    break;
                case "FSAA=8":
                    comboBoxAntialiasing.SelectedIndex = 3;
                    break;
                default:
                    comboBoxAntialiasing.SelectedIndex = 0;
                    break;

            }


            if (ogreConfig.FullScreen.Contains("Yes"))
            {
                checkBoxFullScreen.Checked = true;
            }
            else
            {
                checkBoxFullScreen.Checked = false;
            }

            if (ogreConfig.VSync.Contains("Yes"))
            {
                checkBoxVSync.Checked = true;
            }
            else
            {
                checkBoxVSync.Checked = false;
            }



            switch (ogreConfig.VideoMode)
            {
                case "Video Mode=640 x 480 @ 16-bit colour":
                    comboBoxVideoMode.SelectedIndex = 0;
                    break;
                case "Video Mode=800 x 600 @ 16-bit colour":
                    comboBoxVideoMode.SelectedIndex = 1;
                    break;
                case "Video Mode=1024 x 768 @ 16-bit colour":
                    comboBoxVideoMode.SelectedIndex = 2;
                    break;
                case "Video Mode=1280 x 800 @ 16-bit colour":
                    comboBoxVideoMode.SelectedIndex = 3;
                    break;
                case "Video Mode=1366 x 768 @ 16-bit colour":
                    comboBoxVideoMode.SelectedIndex = 4;
                    break;
                case "Video Mode=640 x 480 @ 32-bit colour":
                    comboBoxVideoMode.SelectedIndex = 5;
                    break;
                case "Video Mode=800 x 600 @ 32-bit colour":
                    comboBoxVideoMode.SelectedIndex = 6;
                    break;
                case "Video Mode=1024 x 768 @ 32-bit colour":
                    comboBoxVideoMode.SelectedIndex = 7;
                    break;
                case "Video Mode=1280 x 800 @ 32-bit colour":
                    comboBoxVideoMode.SelectedIndex = 8;
                    break;
                case "Video Mode=1366 x 768 @ 32-bit colour":
                    comboBoxVideoMode.SelectedIndex = 9;
                    break;
                default:
                    comboBoxVideoMode.SelectedIndex = 0;
                    break;

            }








            appSettings = new XmlDocument();
            try
            {
                appSettings.Load("AppSettings.xml");

                root = appSettings.GetElementsByTagName("SceneView_AppSettings")[0];
                ApplicationSettings = root.SelectSingleNode("ApplicationSettings");
                if (ApplicationSettings != null)
                {
                     AllowDevConsole = ApplicationSettings.SelectSingleNode("AllowDevConsole");
                     SandBoxMode = ApplicationSettings.SelectSingleNode("SandBoxMode");
                     UseHardwarePhysX = ApplicationSettings.SelectSingleNode("UseHardwarePhysX");
                     UseShadows = ApplicationSettings.SelectSingleNode("UseShadows");
                     LogBenchmark = ApplicationSettings.SelectSingleNode("LogBenchmark");
                     ServerAddress = ApplicationSettings.SelectSingleNode("ServerAddress");
                     ServerPort = ApplicationSettings.SelectSingleNode("Port");

                    if (AllowDevConsole.InnerText.Equals("true"))
                    {
                        checkBoxDevConsole.Checked = true;
                    }
                    else
                    {
                        checkBoxDevConsole.Checked = false;
                    }

                    if (SandBoxMode.InnerText.Equals("true"))
                    {
                        comboBoxInstance.SelectedIndex = 1;
                    }
                    else
                    {
                        comboBoxInstance.SelectedIndex = 0;
                    }

                    if (UseHardwarePhysX.InnerText.Equals("true"))
                    {
                        checkBoxPhysX.Checked = true;
                    }
                    else
                    {
                        checkBoxPhysX.Checked = false;
                    }

                    if (UseShadows.InnerText.Equals("true"))
                    {
                        checkBoxUseShadows.Checked = true;
                    }
                    else
                    {
                        checkBoxUseShadows.Checked = false;
                    }

                    if (LogBenchmark.InnerText.Equals("true"))
                    {
                        checkBoxBenchmark.Checked = true;
                    }
                    else
                    {
                        checkBoxBenchmark.Checked = false;
                    }


                    textBoxServerAddress.Text = ServerAddress.InnerText;
                    textBoxServerPort.Text = ServerPort.InnerText;

                }
            }
            catch(Exception e)
            {
                MessageBox.Show("No valid Appsettings.xml found!");
                appSettings = null;
                checkBoxDevConsole.Enabled = false;
                checkBoxPhysX.Enabled = false;
                comboBoxInstance.Enabled = false;
                checkBoxUseShadows.Enabled = false;
                checkBoxBenchmark.Enabled = false;
                buttonLaunch.Enabled = false;
                textBoxServerAddress.Enabled = false;
                textBoxServerPort.Enabled = false;
            }
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (appSettings != null)
            {
                saveSettings();
                appSettings.Save("AppSettings.xml");
            }
            Application.Exit();
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void saveSettings()
        {
            if (checkBoxPhysX.Checked)
            {
                UseHardwarePhysX.InnerText = "true";
            }
            else
            {
                UseHardwarePhysX.InnerText = "false";
            }

            if (checkBoxDevConsole.Checked)
            {
                AllowDevConsole.InnerText = "true";
            }
            else
            {
                AllowDevConsole.InnerText = "false";
            }

            if (comboBoxInstance.SelectedIndex == 0)
            {
                SandBoxMode.InnerText = "false";
            }
            else
            {
                SandBoxMode.InnerText = "true";
            }


            if (checkBoxUseShadows.Checked)
            {
               UseShadows.InnerText = "true";
            }
            else
            {
                UseShadows.InnerText = "false";
            }


            if (checkBoxBenchmark.Checked)
            {
                LogBenchmark.InnerText = "true";
            }
            else
            {
                LogBenchmark.InnerText = "false";
            }

            ServerAddress.InnerText = textBoxServerAddress.Text;
            ServerPort.InnerText = textBoxServerPort.Text;
        }

        private void buttonLaunch_Click(object sender, EventArgs e)
        {
            
            try
            {
                if (appSettings != null)
                {
                    saveSettings();
                    appSettings.Save("AppSettings.xml");
                }
                ogreConfig.writeConfig();
                System.Diagnostics.Process.Start("SceneView.exe");
                Application.Exit();
            }
            catch (Exception ex)
            {
                MessageBox.Show("No SceneView.exe found!","Error");
            }

        }

        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show("SceneView application loader\n\nGyori Szabolcs\n2011","About");
        }

        private void SceneViewLoader_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (appSettings != null)
            {
                saveSettings();
                appSettings.Save("AppSettings.xml");
            }
            ogreConfig.writeConfig();
        }

        private void comboBoxRenderingSysytem_SelectedIndexChanged(object sender, EventArgs e)
        {
            ogreConfig.RenderSystem = comboBoxRenderingSysytem.SelectedItem.ToString();
        }

        private void comboBoxAntialiasing_SelectedIndexChanged(object sender, EventArgs e)
        {
            ogreConfig.FSAA = comboBoxAntialiasing.SelectedItem.ToString();
        }

        private void checkBoxFullScreen_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBoxFullScreen.Checked)
            {
                ogreConfig.FullScreen = "Full Screen=Yes";
            }
            else
            {
                ogreConfig.FullScreen = "Full Screen=No";
            }
        }

        private void checkBoxVSync_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBoxVSync.Checked)
            {
                ogreConfig.VSync = "VSync=Yes";
            }
            else
            {
                ogreConfig.VSync = "VSync=No";
            }
        }

        private void comboBoxVideoMode_SelectedIndexChanged(object sender, EventArgs e)
        {
            ogreConfig.VideoMode = comboBoxVideoMode.SelectedItem.ToString();
        }

        private void checkBoxBenchmark_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBoxBenchmark.Checked)
            {
                labelInstance.Enabled = false;
                comboBoxInstance.SelectedIndex = 1;
                comboBoxInstance.Enabled = false;

                labelServerAddress.Enabled = false;
                labelServerPort.Enabled = false;
                textBoxServerAddress.Enabled = false;
                textBoxServerPort.Enabled = false;
            }
            else
            {
                labelInstance.Enabled = true;
                comboBoxInstance.Enabled = true;

                labelServerAddress.Enabled = true;
                labelServerPort.Enabled = true;
                textBoxServerAddress.Enabled = true;
                textBoxServerPort.Enabled = true;
            }

        }
    }
}
